using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

namespace nToolbox
{
	public static class StringHelper
	{
		/// <summary>
		/// Returns the substring of s starting at the beginning up to the start of substring c.
		/// If c is not present in s, returns all of s.
		/// </summary>
		public static string UpTo(string s, string c)
		{
			if (s == null)
				throw new ArgumentNullException("s");

			if (c == null)
				throw new ArgumentNullException("c");

			int pos = s.IndexOf(c);
			return pos == -1 ? s : s.Substring(0, pos);
		}

		/// <summary>
		/// Rerturns the substring of s starting after c.
		/// If c is not present in s, returns all of s.
		/// </summary>
		public static string After(string s, string c)
		{
			if (s == null)
				throw new ArgumentNullException("s");

			if (c == null)
				throw new ArgumentNullException("c");

			int index = s.IndexOf(c);
			return index == -1 ? s : s.Substring(index + c.Length);
		}

		/// <summary>
		/// Returns the substring with the first min(charcount, s.Length) characters from string s
		/// </summary>
		public static string First(string s, int charcount)
		{
			if (charcount < 0)
				throw new ArgumentOutOfRangeException("s", charcount, "Count cannot be less than 0.");

			if (s == null) return "";
			return s.Substring(0, Math.Min(s.Length, charcount));
		}

		/// <summary>
		/// Trims the string representation of the given object and returns it. If the object or its string representation is null, returns the empty string.
		/// </summary>
		public static string NullTrim(object o)
		{
			return o == null ? "" : NullTrim(o.ToString());
		}

		/// <summary>
		/// Trims the given string and returns it. If the given string is null, return the empty string.
		/// </summary>
		/// <param name="s">The string to trim.</param>
		/// <returns>The trimmed string.</returns>
		public static string NullTrim(string s)
		{
			return s == null ? "" : s.Trim();
		}

		/// <summary>
		/// Joins the list of objects in items using the given delimiter.
		/// Items are converted to strings using object.ToString().
		/// null items are skipped.
		/// </summary>
		public static string Concat(string delimiter, IEnumerable items)
		{
			bool first = true;
			StringBuilder sb = new StringBuilder();

			foreach (object item in items)
			{
				string asString = NullTrim(item);
				if (asString == "")
					continue;

				if (!first)
					sb.Append(delimiter);
				else
					first = false;

				sb.Append(asString);
			}
			return sb.ToString();
		}

		/// <summary>
		/// Joins the list of objects in values using the given delimiter.
		/// Items are converted to strings using object.ToString().
		/// null items are skipped.
		/// </summary>
		public static string Concat(string delimiter, params object[] values)
		{
			return Concat(delimiter, (ICollection)values);
		}

        /// <summary>
        /// Get the Left n characters in a string
        /// </summary>        
        public static string Left(string param, int length)            
        {        
            //we start at 0 since we want to get the characters starting from the             
            //left and with the specified lenght and assign it to a variable            
            string result = param.Substring(0, length);            
            return result;      
        }
            
        /// <summary>
        /// Get the Right n characters in a string
        /// </summary>
        /// <param name="param"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Right(string param, int length)
        {        
            //start at the index based on the lenght of the sting minus            
            //the specified lenght and assign it a variable            
            string result = param.Substring(param.Length - length, length);            
            return result;            
        }
                    
        /// <summary>
        /// Get the Middle portion of a string
        /// </summary>        
        public static string Mid(string param, int startIndex, int length)        
        {        
            //start at the specified index in the string ang get N number of            
            //characters depending on the lenght and assign it to a variable            
            string result = param.Substring(startIndex, length);            
            return result;            
        }

        /// <summary>
        /// Checks to see if there are any numbers in the string passed
        /// </summary>        
        public static bool StringContainsNumber(string strInput)
        {
            return Regex.IsMatch(strInput, "\\d");
        }

        public static string Capitalize(string strInput)
        {
            StringBuilder strReturn = new StringBuilder();
            try
            {
                bool boolWhiteSpace = true;
                foreach (char lChar in strInput)
                {
                    if (char.IsWhiteSpace(lChar))
                    {
                        strReturn.Append(lChar);
                        boolWhiteSpace = true;
                    }
                    else if(char.IsLetter(lChar) && boolWhiteSpace)
                    {
                        boolWhiteSpace = false;
                     strReturn.Append(char.ToUpper(lChar));
                    }
                    else
                    {
                        strReturn.Append(char.ToLower(lChar));
                        boolWhiteSpace = false;
                    }                    
                }
                return strReturn.ToString();
            }
            catch 
            {
                if (strInput == null)
                    return "";
                else
                   return strInput;
            }
        }                     
	}
}